package com.baicun.wish.spiders;

import com.baicun.wish.sink.SinkToCsv;
import com.baicun.wish.sink.SinkToCsv2;
import com.baicun.wish.utils.driver.WebDriverUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author baicun
 * @description  废的
 * 负责爬取 https://gkcx.eol.cn/school/(30-3560)/provinceline 大学录取，专业录取数据
 */
public class CollegeAdmitSpider {

    public static void getCollegeAdmit (int num) throws Exception {

        ChromeDriver chromeDriver = WebDriverUtil.openChrome();
        chromeDriver.get("https://gkcx.eol.cn/school/" + num + "/provinceline");

        //隐式等待
        chromeDriver.manage().timeouts().implicitlyWait(800, TimeUnit.MILLISECONDS);

        //数据获取  表格的数据


        String collegeName = ElementUtil.isExist(chromeDriver,By.className("line1-schoolName")) ? chromeDriver.findElement(By.className("line1-schoolName")).getText() : "";

//        List<WebElement> dropElements = chromeDriver.findElements(By.className("scoreLine-dropDown")).get(0).findElements(By.className("ant-select-selection"));

//        dropElements.get(0).click();
//        String string2 = "";
//        while (true) {
//
//                chromeDriver.switchTo().activeElement().sendKeys(Keys.ENTER);
//
//                String active2 = chromeDriver.switchTo().activeElement().getText();
//
//                System.out.println(active2);
//
//                if (string2.contains(active2)){
//                    break;
//                }
//                string2 += active2;
//
//                dropElements.get(1).click();
//                String string3 = "";
//                while (true){
//
//                    boolean flag = true;
//
//                    chromeDriver.switchTo().activeElement().sendKeys(Keys.ENTER);
//
//                    String active3 = chromeDriver.switchTo().activeElement().getText();
//                    if (string3.contains(active3)){
//                        flag = false;
//
//                    }
//                    if (flag){
//                        System.out.println(active3+ "可以拿数据！");
//                    }
//
//                    if (string3.contains(active3)){
//                        break;
//                    }
//
//                    string3 += active3;
//
//                    dropElements.get(1).click();
//
//                    chromeDriver.switchTo().activeElement().sendKeys(Keys.DOWN);
//
//
//
//                }
//
//
//                dropElements.get(0).click();
//
//                chromeDriver.switchTo().activeElement().sendKeys(Keys.DOWN);
//
//
//        }

        try{
            List<WebElement> majorDropElements = chromeDriver.findElements(By.className("scoreLine-dropDown")).get(2).findElements(By.className("ant-select-arrow"));

            String activeP;
            String activeT;
            String activeY;

//        majorDropElements.get(0).click();
            chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(0));
            StringBuilder stringP = new StringBuilder();
            while(true){
                chromeDriver.switchTo().activeElement().sendKeys(Keys.ENTER);
                activeP = chromeDriver.switchTo().activeElement().getText();
//            System.out.println(activeP);
                if (stringP.toString().contains(activeP)){ break; }
                stringP.append(activeP);
//            majorDropElements.get(1).click();
                chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(1));
                StringBuilder stringT = new StringBuilder();
                while (true) {

                    chromeDriver.switchTo().activeElement().sendKeys(Keys.ENTER);
                    activeT = chromeDriver.switchTo().activeElement().getText();
//                System.out.println(activeT);
                    if (stringT.toString().contains(activeT)){ break; }
                    stringT.append(activeT);
//                majorDropElements.get(2).click();
                    chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(2));
                    StringBuilder stringY = new StringBuilder();
                    while (true){

                        boolean flag = true;

                        chromeDriver.switchTo().activeElement().sendKeys(Keys.ENTER);
                        chromeDriver.manage().timeouts().implicitlyWait(200, TimeUnit.MILLISECONDS);
                        activeY = chromeDriver.switchTo().activeElement().getText();
                        if (stringY.toString().contains(activeY)){
                            flag = false;
                        }
                        if (flag){
                            // 拿数据
                            try{
                                WebElement div = chromeDriver.findElement(By.className("major_score_table"));
                                WebElement tbody = div.findElement(By.tagName("tbody"));

                                List<WebElement> trs = tbody.findElements(By.tagName("tr"));

                                for (int i = 1; i< trs.size() ; i++){
                                    List<WebElement> tds = trs.get(i).findElements(By.tagName("td"));

//                                    System.out.println(
//                                            collegeName+","+activeP+","+activeT+","+activeY+","+
//                                                    tds.get(0).getText()+","+
//                                                    tds.get(1).getText()+ ","+
//                                                    tds.get(2).getText()+ "," +
//                                                    tds.get(3).getText()+","+
//                                                    tds.get(4).getText()+","+
//                                                    tds.get(5).getText()+",");

                                    SinkToCsv2.writeContent(new String[]{collegeName,activeP,activeT,activeY,
                                            tds.get(0).getText(),
                                            tds.get(1).getText(),
                                            tds.get(2).getText(),
                                            tds.get(3).getText(),
                                            tds.get(4).getText(),
                                            tds.get(5).getText()});
                                }
                            }catch (Exception e){
                                System.out.println("没有table项！");
                            }

                        }

                        if (stringY.toString().contains(activeY)){ break; }
                        stringY.append(activeY);

//                    majorDropElements.get(2).click();
                        chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(2));
                        chromeDriver.switchTo().activeElement().sendKeys(Keys.DOWN);

                    }
//                majorDropElements.get(1).click();
                    chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(1));
                    chromeDriver.switchTo().activeElement().sendKeys(Keys.DOWN);


                }
//            majorDropElements.get(0).click();
                chromeDriver.executeScript("arguments[0].click();", majorDropElements.get(0));
                chromeDriver.switchTo().activeElement().sendKeys(Keys.DOWN);

            }

        }catch (Exception e){
            System.out.println("出错了！");
        }




    }

}
